/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.securecreditlib.test;

import java.math.BigDecimal;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.ekeyman.securecreditlib.business.CardTransactionBO;
import com.ekeyman.securecreditlib.business.CreditCardBO;
import com.ekeyman.securecreditlib.business.RegistryBO;
import com.ekeyman.securecreditlib.domain.CardTransaction;
import com.ekeyman.securecreditlib.domain.CreditCard;
import com.ekeyman.securecreditlib.domain.Registry;
import com.ekeyman.securecreditlib.service.EkeymanService;
import com.ekeyman.securecreditlib.util.SecureCreditUtils;
import com.ekeymanlib.dto.EncryptionKeys;

public class SetupJqueryTestsUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext-lib.xml"};
	ClassPathXmlApplicationContext ctx;
	
	private CreditCardBO creditCardBO;
	private CardTransactionBO cardTransactionBO;
	private RegistryBO registryBO;
	private EkeymanService ekeymanService;
	
	@Test
	public void testEnd2End() {
		
		System.out.println("testEnd2End entered...");
		
		Registry registry = getRegistryBO().getRegistry();
		
		if(registry != null && registry.getPublicKey() != null  && !registry.getPublicKey().equalsIgnoreCase("")){
			
			EncryptionKeys encryptionKeys = getEkeymanService().getEncryptionKeys(
					"safetyword", 
					registry.getPublicKey(), 
					registry.getPrivateKey(),
					"App Server");
		
			String clearSafetyword = "";
			
			try{
				clearSafetyword = SecureCreditUtils.decrypt(encryptionKeys, registry.getCipherSafetyword());
			}
			catch(Exception e){
				// Sometimes an exception can occur when trying to decrypt without the original keys!
				// This shouldn't occur in this scenario
				clearSafetyword = "***BOGUS***";
			}

		
			assert registry.getClearSafetyword().equalsIgnoreCase(clearSafetyword) :
				"registry.getClearSafetyword(): " + 
				registry.getClearSafetyword() +
				" does not equal clearSafetyword: " 
				+ clearSafetyword + " ***This is normal if new private key was generated***";
		
			try {
		
				CreditCard newCreditCard1 = new CreditCard();
				newCreditCard1.setCreditCardNumber("7776667890123456");
				newCreditCard1.setCardType("Visa");
				newCreditCard1.setFourDigits("3456");
				newCreditCard1.setExpirationDate("12/2014");
				newCreditCard1.setNameOnCard("Skip Carlson");
				getCreditCardBO().saveCreditCard(newCreditCard1);
				
				CardTransaction cardTransaction11 = new CardTransaction();
				//cardTransaction11.setCreditCard(newCreditCard1);
				cardTransaction11.setTransactionNumber("0000989898980000089898");
				cardTransaction11.setAmount(new BigDecimal("1000.11"));
				cardTransaction11.setPostDate("12/06/2010");
				cardTransaction11.setTransactionDate("12/04/2010");
				cardTransaction11.setVendorCode("BELK STORE 18965434");
				//getCardTransactionBO().saveCardTransaction(cardTransaction11);
				getCardTransactionBO().saveCardTransaction(Long.toString(newCreditCard1.getId()), cardTransaction11);
				
				CardTransaction cardTransaction12 = new CardTransaction();
				//cardTransaction12.setCreditCard(newCreditCard1);
				cardTransaction12.setTransactionNumber("77777798900898000898");
				cardTransaction12.setAmount(new BigDecimal("200.00"));
				cardTransaction12.setPostDate("12/08/2010");
				cardTransaction12.setTransactionDate("12/05/2010");
				cardTransaction12.setVendorCode("SEARS RIVER OAKS 1778434");
				//getCardTransactionBO().saveCardTransaction(cardTransaction12);
				getCardTransactionBO().saveCardTransaction(Long.toString(newCreditCard1.getId()), cardTransaction12);
				
				
				CardTransaction cardTransaction13 = new CardTransaction();
				//cardTransaction13.setCreditCard(newCreditCard1);
				cardTransaction13.setTransactionNumber("6354500056676767676");
				cardTransaction13.setAmount(new BigDecimal("120.00"));
				cardTransaction13.setPostDate("12/08/2010");
				cardTransaction13.setTransactionDate("12/05/2010");
				cardTransaction13.setVendorCode("BEST BUY CAL CITY 88865488");
				//getCardTransactionBO().saveCardTransaction(cardTransaction13);
				getCardTransactionBO().saveCardTransaction(Long.toString(newCreditCard1.getId()), cardTransaction13);
				
			}
			catch(Exception e){
				System.out.println("testEnd2End...ex: " + e.getMessage());
			}

		}
		else {
			System.out.println("testEnd2End...Regsitry inaccessible");
		}
		
		System.out.println("testEnd2End exited...");
	}
	
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		
		setRegistryBO((RegistryBO)ctx.getBeanFactory().getBean("registryBO"));
		setEkeymanService((EkeymanService)ctx.getBeanFactory().getBean("ekeymanService"));
		setCreditCardBO((CreditCardBO)ctx.getBeanFactory().getBean("creditCardBO"));
		setCardTransactionBO((CardTransactionBO)ctx.getBeanFactory().getBean("cardTransactionBO"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}


	public void setCreditCardBO(CreditCardBO creditCardBO) {
		this.creditCardBO = creditCardBO;
	}


	public CreditCardBO getCreditCardBO() {
		return creditCardBO;
	}


	public void setCardTransactionBO(CardTransactionBO cardTransactionBO) {
		this.cardTransactionBO = cardTransactionBO;
	}


	public CardTransactionBO getCardTransactionBO() {
		return cardTransactionBO;
	}


	public void setRegistryBO(RegistryBO registryBO) {
		this.registryBO = registryBO;
	}


	public RegistryBO getRegistryBO() {
		return registryBO;
	}


	public void setEkeymanService(EkeymanService ekeymanService) {
		this.ekeymanService = ekeymanService;
	}


	public EkeymanService getEkeymanService() {
		return ekeymanService;
	}

}
